﻿namespace Cellfish.Test.Fakes.System.IO
{
    using global::System;
    using global::System.IO;
    using global::System.Threading;
    using global::System.Threading.Tasks;

    using Microsoft.VisualStudio.TestTools.UnitTesting;

    /// <summary>
    /// Implements a fake stream to be used in tests.
    /// </summary>
    public class FakeStream : Stream
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="FakeStream"/> class.
        /// </summary>
        public FakeStream()
        {
            this.Behaviors = new FakeBehaviors();
        }

        /// <summary>
        /// Gets the fake behaviors.
        /// </summary>
        /// <value>
        /// The fake behaviors.
        /// </value>
        public FakeBehaviors Behaviors { get; private set; }

        /// <summary>
        /// When overridden in a derived class, gets a value indicating whether the current stream supports reading.
        /// </summary>
        /// <returns>true if the stream supports reading; otherwise, false.</returns>
        public override bool CanRead
        {
            get
            {
                return this.Behaviors.CanRead;
            }
        }

        /// <summary>
        /// When overridden in a derived class, gets a value indicating whether the current stream supports seeking.
        /// </summary>
        /// <returns>true if the stream supports seeking; otherwise, false.</returns>
        public override bool CanSeek
        {
            get
            {
                return this.Behaviors.CanRead;
            }
        }

        /// <summary>
        /// When overridden in a derived class, gets a value indicating whether the current stream supports writing.
        /// </summary>
        /// <returns>true if the stream supports writing; otherwise, false.</returns>
        public override bool CanWrite
        {
            get
            {
                return this.Behaviors.CanWrite;
            }
        }

        /// <summary>
        /// When overridden in a derived class, gets the length in bytes of the stream.
        /// </summary>
        /// <returns>A long value representing the length of the stream in bytes.</returns>
        public override long Length
        {
            get
            {
                return this.Behaviors.Length;
            }
        }

        /// <summary>
        /// When overridden in a derived class, gets or sets the position within the current stream.
        /// </summary>
        /// <returns>The current position within the stream.</returns>
        public override long Position { get; set; }

        /// <summary>
        /// Gets a value that determines whether the current stream can time out.
        /// </summary>
        /// <returns>A value that determines whether the current stream can time out.</returns>
        public override bool CanTimeout
        {
            get
            {
                return this.Behaviors.CanTimeout;
            }
        }

        /// <summary>
        /// Gets or sets a value, in miliseconds, that determines how long the stream will attempt to read before timing out.
        /// </summary>
        /// <returns>A value, in miliseconds, that determines how long the stream will attempt to read before timing out.</returns>
        public override int ReadTimeout { get; set; }

        /// <summary>
        /// Gets or sets a value, in miliseconds, that determines how long the stream will attempt to write before timing out.
        /// </summary>
        /// <returns>A value, in miliseconds, that determines how long the stream will attempt to write before timing out.</returns>
        public override int WriteTimeout { get; set; }

        /// <summary>
        /// When overridden in a derived class, clears all buffers for this stream and causes any buffered data to be written to the underlying device.
        /// </summary>
        public override void Flush()
        {
            Assert.IsNotNull(this.Behaviors.Flush, "Unexpected call to Flush().");
            this.Behaviors.Flush();
        }

        /// <summary>
        /// Asynchronously clears all buffers for this stream, causes any buffered data to be written to the underlying device, and monitors cancellation requests.
        /// </summary>
        /// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="P:System.Threading.CancellationToken.None" />.</param>
        /// <returns>
        /// A task that represents the asynchronous flush operation.
        /// </returns>
        public override Task FlushAsync(CancellationToken cancellationToken)
        {
            Assert.IsNotNull(this.Behaviors.FlushAsync, "Unexpected call to FlushAsync(...).");
            return this.Behaviors.FlushAsync(cancellationToken);
        }

        /// <summary>
        /// When overridden in a derived class, sets the position within the current stream.
        /// </summary>
        /// <param name="offset">A byte offset relative to the <paramref name="origin" /> parameter.</param>
        /// <param name="origin">A value of type <see cref="T:System.IO.SeekOrigin" /> indicating the reference point used to obtain the new position.</param>
        /// <returns>
        /// The new position within the current stream.
        /// </returns>
        public override long Seek(long offset, SeekOrigin origin)
        {
            Assert.IsNotNull(this.Behaviors.Seek, "Unexpected call to Seek({0}, {1})", offset, origin);
            return this.Behaviors.Seek(offset, origin);
        }

        /// <summary>
        /// When overridden in a derived class, sets the length of the current stream.
        /// </summary>
        /// <param name="value">The desired length of the current stream in bytes.</param>
        public override void SetLength(long value)
        {
            Assert.IsNotNull(this.Behaviors.SetLength, "Unexpected call to SetLength({0})", value);
            this.Behaviors.SetLength(value);
        }

        /// <summary>
        /// When overridden in a derived class, reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.
        /// </summary>
        /// <param name="buffer">An array of bytes. When this method returns, the buffer contains the specified byte array with the values between <paramref name="offset" /> and (<paramref name="offset" /> + <paramref name="count" /> - 1) replaced by the bytes read from the current source.</param>
        /// <param name="offset">The zero-based byte offset in <paramref name="buffer" /> at which to begin storing the data read from the current stream.</param>
        /// <param name="count">The maximum number of bytes to be read from the current stream.</param>
        /// <returns>
        /// The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.
        /// </returns>
        public override int Read(byte[] buffer, int offset, int count)
        {
            Assert.IsNotNull(this.Behaviors.Read, "Unexpected call to Read({0}, {1}, {2})", buffer == null ? "null" : string.Format("byte[{0}]", buffer.Length), offset, count);
            return this.Behaviors.Read(buffer, offset, count);
        }

        /// <summary>
        /// Asynchronously reads a sequence of bytes from the current stream, advances the position within the stream by the number of bytes read, and monitors cancellation requests.
        /// </summary>
        /// <param name="buffer">The buffer to write the data into.</param>
        /// <param name="offset">The byte offset in <paramref name="buffer" /> at which to begin writing data from the stream.</param>
        /// <param name="count">The maximum number of bytes to read.</param>
        /// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="P:System.Threading.CancellationToken.None" />.</param>
        /// <returns>
        /// A task that represents the asynchronous read operation. The value of the task contains the total number of bytes read into the buffer. The result value can be less than the number of bytes requested if the number of bytes currently available is less than the requested number, or it can be 0 (zero) if the end of the stream has been reached.
        /// </returns>
        public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
        {
            Assert.IsNotNull(this.Behaviors.ReadAsync, "Unexpected call to ReadAsync({0}, {1}, {2}, ...)", buffer == null ? "null" : string.Format("byte[{0}]", buffer.Length), offset, count);
            return this.Behaviors.ReadAsync(buffer, offset, count, cancellationToken);
        }

        /// <summary>
        /// When overridden in a derived class, writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
        /// </summary>
        /// <param name="buffer">An array of bytes. This method copies <paramref name="count" /> bytes from <paramref name="buffer" /> to the current stream.</param>
        /// <param name="offset">The zero-based byte offset in <paramref name="buffer" /> at which to begin copying bytes to the current stream.</param>
        /// <param name="count">The number of bytes to be written to the current stream.</param>
        public override void Write(byte[] buffer, int offset, int count)
        {
            Assert.IsNotNull(this.Behaviors.Write, "Unexpected call to Write({0}, {1}, {2})", buffer == null ? "null" : string.Format("byte[{0}]", buffer.Length), offset, count);
            this.Behaviors.Write(buffer, offset, count);
        }

        /// <summary>
        /// Asynchronously writes a sequence of bytes to the current stream, advances the current position within this stream by the number of bytes written, and monitors cancellation requests.
        /// </summary>
        /// <param name="buffer">The buffer to write data from.</param>
        /// <param name="offset">The zero-based byte offset in <paramref name="buffer" /> from which to begin copying bytes to the stream.</param>
        /// <param name="count">The maximum number of bytes to write.</param>
        /// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="P:System.Threading.CancellationToken.None" />.</param>
        /// <returns>
        /// A task that represents the asynchronous write operation.
        /// </returns>
        public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
        {
            Assert.IsNotNull(this.Behaviors.WriteAsync, "Unexpected call to WriteAsync({0}, {1}, {2}, ...)", buffer == null ? "null" : string.Format("byte[{0}]", buffer.Length), offset, count);
            return this.Behaviors.WriteAsync(buffer, offset, count, cancellationToken);
        }

        /// <summary>
        /// Releases the unmanaged resources used by the <see cref="T:System.IO.Stream" /> and optionally releases the managed resources.
        /// </summary>
        /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param>
        protected override void Dispose(bool disposing)
        {
            this.Behaviors.Disposed = true;
        }

        /// <summary>
        /// Contains fake behaviors for the fake stream.
        /// </summary>
        public class FakeBehaviors
        {
            /// <summary>
            /// Gets a value indicating whether the fake was closed/disposed or not.
            /// </summary>
            /// <value>
            ///   <c>true</c> if disposed; otherwise, <c>false</c>.
            /// </value>
            public bool Disposed { get; internal set; }

            /// <summary>
            /// Gets or sets a value indicating whether fake can be read or not.
            /// </summary>
            /// <value>
            ///   <c>true</c> if can read; otherwise, <c>false</c>.
            /// </value>
            public bool CanRead { internal get; set; }

            /// <summary>
            /// Gets or sets a value indicating whether fake can seeked or not.
            /// </summary>
            /// <value>
            ///   <c>true</c> if can seek; otherwise, <c>false</c>.
            /// </value>
            public bool CanSeek { internal get; set; }

            /// <summary>
            /// Gets or sets a value indicating whether fake can be written or not.
            /// </summary>
            /// <value>
            ///   <c>true</c> if can write; otherwise, <c>false</c>.
            /// </value>
            public bool CanWrite { internal get; set; }

            /// <summary>
            /// Gets or sets the length.
            /// </summary>
            /// <value>
            /// The length.
            /// </value>
            public long Length { internal get; set; }

            /// <summary>
            /// Gets or sets a value indicating whether fake can timeout or not.
            /// </summary>
            /// <value>
            ///   <c>true</c> if can timeout; otherwise, <c>false</c>.
            /// </value>
            public bool CanTimeout { internal get; set; }

            /// <summary>
            /// Gets or sets the flush behavior.
            /// </summary>
            /// <value>
            /// The flush behavior.
            /// </value>
            public Action Flush { internal get; set; }

            /// <summary>
            /// Gets or sets the flush asynchronous behavior.
            /// </summary>
            /// <value>
            /// The flush asynchronous behavior.
            /// </value>
            public Func<CancellationToken, Task> FlushAsync { internal get; set; }

            /// <summary>
            /// Gets or sets the seek behavior.
            /// </summary>
            /// <value>
            /// The seek behavior.
            /// </value>
            public Func<long, SeekOrigin, long> Seek { internal get; set; }

            /// <summary>
            /// Gets or sets the set length behavior.
            /// </summary>
            /// <value>
            /// The set length behavior.
            /// </value>
            public Action<long> SetLength { internal get; set; }

            /// <summary>
            /// Gets or sets the read behavior.
            /// </summary>
            /// <value>
            /// The read behavior.
            /// </value>
            public Func<byte[], int, int, int> Read { internal get; set; }

            /// <summary>
            /// Gets or sets the write behavior.
            /// </summary>
            /// <value>
            /// The write behavior.
            /// </value>
            public Action<byte[], int, int> Write { internal get; set; }

            /// <summary>
            /// Gets or sets the read asynchronous behavior.
            /// </summary>
            /// <value>
            /// The read asynchronous behavior.
            /// </value>
            public Func<byte[], int, int, CancellationToken, Task<int>> ReadAsync { internal get; set; }

            /// <summary>
            /// Gets or sets the write asynchronous behavior.
            /// </summary>
            /// <value>
            /// The write asynchronous behavior.
            /// </value>
            public Func<byte[], int, int, CancellationToken, Task> WriteAsync { internal get; set; }
        }
    }
}
